<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2019-11-25
 * Time: 20:59
 */

namespace app\api\validate;


use app\lib\exception\ParameterException;
use think\facade\Request;
use think\Validate;

class BaseValidate extends Validate
{
    public function goCheck()
    {
        //获取http参数
        //对参数进行检验
        $request = Request::instance();
        $prams = $request->param();
        $result = $this->batch()->check($prams);
        if(!$result)
        {
            $e = new ParameterException([
                'msg' => $this->error
            ]);
            throw $e;
        }
        else{
            return true;
        }
    }
    //正整数
    protected function isPositiveInteger($value)
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        } else {
            return false;
        }
    }

    //不为空
    protected function isNotEmpty($value)
    {
        if (empty($value)) {
            return false;
        } else {
            return true;
        }
    }

    //判断手机号
    protected function isMobile($value)
    {
        $rule = '^1(3|4|5|7|8|9)[0-9]\d{8}$^';
        $result = preg_match($rule,$value);
        if($result){
            return true;
        }else{
            return false;
        }
    }

    //参数里面不能出现user_id或者uid
    public function getDataByRule($arrays)
    {
        if(array_key_exists('user_id',$arrays) |
            array_key_exists('uid',$arrays))
        {
            throw new ParameterException(
                [
                    'msg' => '参数中包含有非法的参数名user_id或者uid'
                ]);
        }
        $newArray = [];
        foreach ($this->rule as $key => $value)
        {
            $newArray[$key] = $arrays[$key];
        }
        return $newArray;
    }
}